package com.ushine.web.template.validation.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ushine.web.component.authority.DataFilterTypeEnum;
import com.ushine.web.component.contant.UniquenessEnum;
import com.ushine.web.component.exception.ServiceException;
import com.ushine.web.component.http.HttpResponseVO;
import com.ushine.web.component.page.PageRequestQuery;
import com.ushine.web.component.submit.LocalLockRequest;
import com.ushine.web.template.validation.model.ValidationDO;
import com.ushine.web.template.validation.service.IValidationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Optional;

/**
 * @Author: yls
 * @Date 2022/3/18 9:06
 * @Description: 数据校验测试
 * Valid：注解用于校验，所属包为：javax.validation.Valid。@Valid不提供分组功能
 * Validated Validated是@Valid 的一次封装，是Spring提供的校验机制使用。
 * @Version 1.0
 **/
@RestController
@Api(tags = "组件模块-测试模板")
@RequestMapping("component/validation")
@Validated
public class ValidationController {

    @Resource
    private IValidationService validationService;

    @ApiOperation(value = "添加数据")
    @PostMapping("insertValidation")
    @LocalLockRequest(key = "component/validation/insertValidation")
    public HttpResponseVO<Object> insertValidation(@RequestBody @Valid ValidationDO validation) throws ServiceException {
        Boolean flag = validationService.insertValidation(validation);
        if (Boolean.TRUE.equals(flag)) {
            return new HttpResponseVO<>().ok("操作成功");
        }
        return new HttpResponseVO<>().ok("操作失败");
    }

    @ApiOperation(value = "更新数据")
    @PutMapping("updateValidation")
    public HttpResponseVO<Object> updateValidation(@RequestBody @Valid ValidationDO validation) throws ServiceException {
        Boolean flag = validationService.updateValidation(validation);
        if (Boolean.TRUE.equals(flag)) {
            return new HttpResponseVO<>().ok("操作成功");
        }
        return new HttpResponseVO<>().ok("操作失败");
    }

    @ApiOperation(value = "删除数据")
    @DeleteMapping("deleteValidation")
    public HttpResponseVO<Object> deleteValidation(@RequestBody @NotEmpty(message = "id集合不能为空") List<Long> ids) throws ServiceException {
        Boolean flag = validationService.deleteValidation(ids);
        if (Boolean.TRUE.equals(flag)) {
            return new HttpResponseVO<>().ok("操作成功");
        }
        return new HttpResponseVO<>().ok("操作失败");
    }


    @ApiOperation(value = "删除数据以及关联数据")
    @DeleteMapping("deleteValidationAndLinkData")
    public HttpResponseVO<Object> deleteValidationAndLinkData(@RequestBody @NotEmpty(message = "id集合不能为空") List<Long> ids) throws ServiceException {
        Boolean flag = validationService.deleteValidationAndLinkData(ids);
        if (Boolean.TRUE.equals(flag)) {
            return new HttpResponseVO<>().ok("操作成功");
        }
        return new HttpResponseVO<>().ok("操作失败");
    }

    @ApiOperation(value = "查询单条数据")
    @GetMapping(value ="getValidation")
    public HttpResponseVO<Object> getValidation(@RequestParam @NotNull(message = "id不能为null") Long id) throws ServiceException {
        Optional<ValidationDO> validation = validationService.getValidation(id);
        if (validation.isPresent()) {
            return new HttpResponseVO<>().ok("操作成功",validation.get());
        }
        return new HttpResponseVO<>().ok("操作失败，未查询到相关的数据！");
    }


    @ApiOperation(value = "分页查询数据")
    @PostMapping("pageValidation")
    public HttpResponseVO<Object> pageValidation(@RequestBody PageRequestQuery<ValidationDO> pageRequestQuery) {
        Page<ValidationDO> validationPage = validationService.listValidationsByPage(pageRequestQuery);
        return new HttpResponseVO<>().ok("操作成功", validationPage);
    }

    @ApiOperation(value = "分支数据")
    @GetMapping("branchDemo")
    public HttpResponseVO<Object> branchDemo(@RequestParam UniquenessEnum firstBranchVar,
                                             @RequestParam  DataFilterTypeEnum secondBranchVar) {
        String result = validationService.branchDemo(firstBranchVar, secondBranchVar);
        return new HttpResponseVO<>().ok("操作成功", result);
    }

}
